<template>
  <div id="system-setting" class="relative">
    <page-loading></page-loading>
    <div class="form-wrap mb20">
      <div class="form-title">{{data.form1.title}}：</div>
      <div v-for="(item, idx) in data.form1.fields" :key="idx">
        <os-switch :item="item" v-if="item.type=='switch'" v-model="ntp_config[idx]"></os-switch>
        <os-input :item="item" v-if="item.type=='input'" v-model="ntp_config[idx]"></os-input>
      </div>
      <div class="value alone-right">
        <div class="btn btn-blue" @click="set_ntp">{{$t('btn.save')}}</div>
      </div>
    </div>
    <div class="form-wrap mb20" v-if="false">
      <div class="form-title">{{data.form2.title}}：</div>
      <div v-for="(item, idx) in data.form2.fields" :key="idx">
        <os-switch :item="item" v-if="item.type=='switch'"></os-switch>
        <os-entry :item="item" v-if="item.type=='select'">
          <el-select class="select-middle"></el-select>
        </os-entry>
        <os-entry :item="item" v-if="item.type=='entry'"></os-entry>
      </div>
      <div class="value alone-right">
        <div class="btn btn-blue">{{$t('btn.save')}}</div>
      </div>
    </div>
  </div>
</template>

<script>

  export default {
    data(){
      return {
        ntp_config: [],
      }
    },
    computed:{
      data(){
        return this.$t('system.settings')
      }
    },
    methods: {
      get_ntp(){
        this.$req.get('/webapi?op=get_ntp').then(res=> {
          console.log(res)
          this.ntp_config = this.obj_to_arr(res, ['enable_ntp_client', 'enable_ntp_server',
          'server_primary', 'server_secondary'])
        })
      },
      set_ntp(){
        let params = this.arr_to_obj(this.ntp_config, ['enable_ntp_client', 'enable_ntp_server',
          'server_primary', 'server_secondary'])
        console.log(params)
        this.proxy_req('post', '/webapi?op=set_ntp', params, res=> {
          console.dir(res);
          this.get_ntp()
        }, true,)
        // this.$req.post('/webapi?op=set_ntp', params ).then(res=> {
        //   console.dir(res);
        //   this.get_ntp()
        //   this.$message({
        //     type: 'success',
        //     message: res.reason,
        //   })
        // })
      }
    },
    created(){
      this.$bus.emit('changeTab', 2)
      this.get_ntp()
    }
  }
</script>

<style lang="scss" scoped>

</style>
